//
//  HeaderView.swift
//  InterviewContent
//
//  Created by apple on 2016/12/18.
//  Copyright © 2016年 interviewContent. All rights reserved.
//

import UIKit


class HeaderView: UIView, UITextFieldDelegate{

    var isHeader : Bool = false
    
    var headerKey = ""
    var headerValue = ""
    var paraerKey = ""
    var paraerValue = ""
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.masksToBounds = true
        layer.borderWidth = 0.7
        layer.shadowOffset = CGSize(width: 0, height: 1)
        layer.shadowOpacity = 4.0
        layer.shadowRadius = 4.0
        layer.shadowColor = UIColor.black.cgColor
        backgroundColor = UIColor.black
        layer.cornerRadius = 10
        
        addSubview(submitBtn)
        addSubview(keyField)
        addSubview(valueFeild)
        addSubview(TitleLB)
        addSubview(lineView)
        addSubview(lineView1)
        
        TitleLB.frame = CGRect(x: 10, y: 10, width: 100, height: 30)
        lineView.frame = CGRect(x: 0, y: 41, width: 500, height: 0.7)
        keyField.frame = CGRect(x: 10, y: 55, width: UIScreen.main.bounds.width/2 - 30, height: 30)
        valueFeild.frame =  CGRect(x: UIScreen.main.bounds.width/2, y: 55, width: UIScreen.main.bounds.width/2 - 30, height: 30)
        lineView1.frame = CGRect(x: 0, y: 100, width: 500, height: 0.7)
        submitBtn.frame = CGRect(x: UIScreen.main.bounds.width/2 - 35, y: 105, width: 50, height: 50)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var TitleLB : UILabel = {
        let label = UILabel()
        label.textColor = UIColor.orange
        label.font.withSize(20)
        label.text = self.isHeader ? "Headers" : "Params"
        return label
    }()
    
    private lazy var lineView : UIView = {
        let lineView = UIView()
        lineView.backgroundColor = UIColor.orange
        return lineView
    }()
    
    private lazy var lineView1 : UIView = {
        let lineView = UIView()
        lineView.backgroundColor = UIColor.orange
        return lineView
    }()

    private lazy var submitBtn : UIButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.orange
        button.setTitle("保存", for: UIControlState.normal)
        button.layer.cornerRadius = 25
        button.addTarget(self, action: #selector(saveValue), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    lazy var keyField : UITextField = {
        let textField = UITextField()
        textField.layer.borderWidth = 0.7
        textField.delegate = self
        var attributes:[String:AnyObject] = NSMutableDictionary() as! [String:AnyObject]
        attributes[NSForegroundColorAttributeName] = UIColor.orange
        //创建带属性的字符串
        let string:NSAttributedString = NSAttributedString.init(string: " key:", attributes: attributes)
        //设置placeholder的属性
        textField.attributedPlaceholder = string
        textField.layer.borderColor = UIColor.orange.cgColor
        textField.layer.cornerRadius = 5
        textField.textColor = UIColor.white
        return textField
    }()
    
    lazy var valueFeild : UITextField = {
        let textField = UITextField()
        textField.layer.borderWidth = 0.7
        textField.delegate = self
        var attributes:[String:AnyObject] = NSMutableDictionary() as! [String:AnyObject]
        attributes[NSForegroundColorAttributeName] = UIColor.orange
        //创建带属性的字符串
        let string:NSAttributedString = NSAttributedString.init(string: " value:", attributes: attributes)
        //设置placeholder的属性
        textField.attributedPlaceholder = string
        textField.layer.borderColor = UIColor.orange.cgColor
        textField.layer.cornerRadius = 5
        textField.textColor = UIColor.white
        return textField
    }()
    
    func addRequestHeader() {
        print("hello")
    }
    // textField delegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    func saveValue() {
        if ((keyField.text?.isEmpty)! || (valueFeild.text?.isEmpty)!) {
            let alertView = UIAlertView()
            alertView.delegate = self
            alertView.title = "title"
            alertView.message = "请输入key或value"
            alertView.addButton(withTitle: "ok")
            alertView.show()
            return
        } else {
            if isHeader {
                headerKey = keyField.text!
                headerValue = valueFeild.text!
            } else {
                paraerKey = keyField.text!
                paraerValue = valueFeild.text!
            }
            
            
            
        }
        
    }
}
